package server.world;

import general.datastructures.Vector2f;

import java.util.HashMap;
import java.util.Vector;


public class RouteStorage 
{

	public class RouteRequest
	{
		
		public RouteRequest(int owner, Vector2f from, Vector2f to, int vehicletype) 
		{
			this.owner = owner;
			this.from = from;
			this.to = to;
			this.vehicletype = vehicletype;
		}
		
		public int owner;
		public Vector2f from;
		public Vector2f to;
		public int vehicletype;
	}
	
	private Vector<RouteRequest> requests = new Vector<RouteRequest>();
	private HashMap<Integer, Vector<Vector2f>> routes = new HashMap<Integer, Vector<Vector2f>>();
	
	public synchronized void requestRoute(Vector2f mc_from, Vector2f mc_to, int vehicletype, int owner)
	{
		requests.add(new RouteRequest(owner, mc_from, mc_to, vehicletype));
	}
	
	public RouteRequest getNextRequest()
	{
		if(!requests.isEmpty())
		{
			return requests.remove(0);
		}
		else
		{
			return null;
		}
	}
	
	public void addRoute(int owner, Vector<Vector2f> wc_route)
	{
		synchronized (routes) {
			routes.put(owner, wc_route);
		}
	}
	
	//TODO wartezeit parametrisieren
	public Vector<Vector2f> getRoute(int owner)
	{
		for(int counter = 0; counter<20; counter++)
		{
			synchronized (routes) {
				if(routes.containsKey(owner))
				{
					return routes.remove(owner);
				}
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
}
